<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单API测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            line-height: 1.6;
        }
        .result {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 20px;
        }
        .error {
            color: red;
        }
        .success {
            color: green;
        }
        button {
            padding: 10px 15px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            margin-right: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        pre {
            background-color: #f8f9fa;
            padding: 10px;
            border-radius: 5px;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <h1>API测试页面</h1>
    
    <div>
        <button onclick="testUsersAPI()">测试用户API</button>
        <button onclick="testRolesAPI()">测试角色API</button>
        <button onclick="testPermissionsAPI()">测试权限API</button>
        <button onclick="testPageViewsAPI()">测试页面访问API</button>
        <button onclick="testAllAPIs()">测试所有API</button>
    </div>
    
    <div id="results"></div>
    
    <script>
        function addResult(title, content, isError = false) {
            const results = document.getElementById('results');
            const resultDiv = document.createElement('div');
            resultDiv.className = 'result ' + (isError ? 'error' : 'success');
            
            const titleElement = document.createElement('h3');
            titleElement.textContent = title;
            resultDiv.appendChild(titleElement);
            
            const contentElement = document.createElement('pre');
            contentElement.textContent = JSON.stringify(content, null, 2);
            resultDiv.appendChild(contentElement);
            
            results.appendChild(resultDiv);
        }
        
        function testUsersAPI() {
            const url = window.location.origin + '/permission-system/api/users';
            console.log('测试用户API:', url);
            
            fetch(url)
                .then(response => {
                    console.log('用户API响应状态:', response.status);
                    return response.json();
                })
                .then(data => {
                    console.log('用户数据:', data);
                    addResult('用户API响应', data);
                })
                .catch(error => {
                    console.error('获取用户数据失败:', error);
                    addResult('用户API错误', error.toString(), true);
                });
        }
        
        function testRolesAPI() {
            const url = window.location.origin + '/permission-system/api/roles';
            console.log('测试角色API:', url);
            
            fetch(url)
                .then(response => {
                    console.log('角色API响应状态:', response.status);
                    return response.json();
                })
                .then(data => {
                    console.log('角色数据:', data);
                    addResult('角色API响应', data);
                })
                .catch(error => {
                    console.error('获取角色数据失败:', error);
                    addResult('角色API错误', error.toString(), true);
                });
        }
        
        function testPermissionsAPI() {
            const url = window.location.origin + '/permission-system/api/permissions';
            console.log('测试权限API:', url);
            
            fetch(url)
                .then(response => {
                    console.log('权限API响应状态:', response.status);
                    return response.json();
                })
                .then(data => {
                    console.log('权限数据:', data);
                    addResult('权限API响应', data);
                })
                .catch(error => {
                    console.error('获取权限数据失败:', error);
                    addResult('权限API错误', error.toString(), true);
                });
        }
        
        function testPageViewsAPI() {
            const url = window.location.origin + '/permission-system/api/stats/page-views';
            console.log('测试页面访问API:', url);
            
            fetch(url)
                .then(response => {
                    console.log('页面访问API响应状态:', response.status);
                    return response.json();
                })
                .then(data => {
                    console.log('页面访问数据:', data);
                    addResult('页面访问API响应', data);
                })
                .catch(error => {
                    console.error('获取页面访问数据失败:', error);
                    addResult('页面访问API错误', error.toString(), true);
                });
        }
        
        function testAllAPIs() {
            document.getElementById('results').innerHTML = '';
            testUsersAPI();
            testRolesAPI();
            testPermissionsAPI();
            testPageViewsAPI();
        }
    </script>
</body>
</html>